import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Zombie Enemy from Castlevania II
 */
public class Zombie  extends Mover implements Runnable
{
    private GreenfootImage[] walkLeft;
    private GreenfootImage[] death;;
    private retroWorld theWorld;
    private int walkCounter = 0;
    private int vSpeed;
    private int acceleration;
    private int halfImg;
    
    
    private boolean alive;
    
    Simon simon;
    Health health;
   
    private volatile Thread t;

    public Zombie()
    {
        walkLeft = new GreenfootImage[2];
        death = new GreenfootImage[4];
        
        alive=true;
   
        walkCounter = 0;
        vSpeed = 0; //Current falling speed
        acceleration = 1; //Falling speed
            
        halfImg = getImage().getHeight()/2; //Coordinates of Zombie's feet
        
        
        walkLeft[0] = new GreenfootImage("ZombieLeft1.png");
        walkLeft[1] = new GreenfootImage("ZombieLeft2.png");
        
        for (int i = 0; i < 4; i++)
            death[i] = new GreenfootImage("CVDeath" + (i + 1) + ".png");
    }
    
    public void run()
    {
        while(alive)
        {
            move();
            fall();
            cycleImage();
            checkCollision();
            
            if(alive)
            if (getX() < 5){
                removeFromWorld();
            }
            
            try {
                t.sleep(20);
            } catch (InterruptedException e) {}
        }
    }
    
    public void addedToWorld(World world)
    {
        theWorld = (retroWorld)world;
        simon = theWorld.getSimon();
        health = theWorld.getHealth();
        t = new Thread(this);
        t.start();
    }
    
    private void cycleImage()
    {
        if (getX() % 10 == 0)
        {
            setImage(walkLeft[walkCounter % 2]);
            walkCounter++;
        }
    }
    
    private synchronized void checkCollision()
    {
        if (intersects(simon))
        {
            if (simon.isCurrentlyWhipping()){
                removeFromWorld();
                return;
            }
            else if (simon.isCurrentlyWhipping() == false){
                health.takeDamage();
            }
        }
    }
    
    public void removeFromWorld()
    {
        try {
        
            setImage(death[0]);
            t.sleep(100);
            setImage(death[1]);
            t.sleep(100);
            setImage(death[2]);
            t.sleep(100);
            setImage(death[3]);
            t.sleep(100);
            
        } catch (InterruptedException e) {}
        t=null;
        theWorld.removeObject(this);
        alive=false;
    }
    
    private void fall()
    {
        vSpeed = vSpeed + acceleration;
        
        //detect collision with ground pixel by pixel
        for (int i = 0; i <= vSpeed; i++)
        {
            if (alive && theWorld.isGround(getX(), getY() + halfImg + i))
            {
                vSpeed = 0;
                setLocation(getX(), getY() + i);
            }

        }
        
        setLocation (getX(), (getY() + vSpeed));
    }
}
